//
//  SZUserAccount.m
//  Weibo
//
//  Created by Zahi on 2017/7/26.
//  Copyright © 2017年 Zahi. All rights reserved.
//

#import "SZUserAccount.h"

@interface SZUserAccount()<NSCoding>



@end

@implementation SZUserAccount


/// 过期秒数转为日期
- (void)setExpires_in:(NSInteger)expires_in
{
    _expires_in = expires_in;
    
    _expiersDate = [NSDate dateWithTimeIntervalSinceNow:expires_in];
    
}


/// 归档
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.access_token forKey:@"access_token"];
    [aCoder encodeObject:self.expiersDate forKey:@"expiersDate"];
    [aCoder encodeInteger:self.uid forKey:@"uid"];
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.avatar_large forKey:@"avatar_large"];
    
}

/// 解档
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    
    if (self = [super init]) {
        
        self.access_token = [aDecoder decodeObjectForKey:@"access_token"];
        self.expiersDate = [aDecoder decodeObjectForKey:@"expiersDate"];
        self.uid = [aDecoder decodeIntegerForKey:@"uid"];
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.avatar_large = [aDecoder decodeObjectForKey:@"avatar_large"];
        
    }
    return self;
}

#pragma mark - 外界保存用户信息和读取用户信息
- (void)saveAccount
{
    [NSKeyedArchiver archiveRootObject:self toFile:kAccountPath];
}

+ (instancetype)readAccount
{
    SZUserAccount *account = [SZUserAccount new];
    account = [NSKeyedUnarchiver unarchiveObjectWithFile:kAccountPath];
    return account;
}



// 构造方法
- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        // 字典转模型
        [self setValuesForKeysWithDictionary:dict];
        
    }
    return self;
}


+ (instancetype)accountWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}


- (void)setValue:(id)value forUndefinedKey:(NSString *)key{}
@end
